package com.kakarote.oa.controller;

import com.kakarote.common.log.annotation.OperateLog;
import com.kakarote.common.log.entity.OperationLog;
import com.kakarote.common.log.entity.OperationResult;
import com.kakarote.common.log.enums.ApplyEnum;
import com.kakarote.common.log.enums.OperateObjectEnum;
import com.kakarote.common.log.enums.OperateTypeEnum;
import com.kakarote.core.common.Result;
import com.kakarote.core.entity.BasePage;
import com.kakarote.oa.entity.BO.OaLogTemplateConfigQueryBO;
import com.kakarote.oa.entity.BO.OaLogTemplateConfigSaveBO;
import com.kakarote.oa.entity.PO.OaLogTemplateConfigSort;
import com.kakarote.oa.entity.VO.OaLogTemplateConfigListVO;
import com.kakarote.oa.entity.VO.OaLogTemplateConfigVO;
import com.kakarote.oa.service.IOaLogTemplateConfigService;
import com.kakarote.oa.service.IOaLogTemplateConfigSortService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wwl
 * @date 2022/7/9 11:11
 */
@RestController
@RequestMapping("/oaLogTemplate")
@Api(tags = "日志模板")
public class OaLogTemplateController {

    @Autowired
    private IOaLogTemplateConfigService oaLogTemplateConfigService;

    @Autowired
    private IOaLogTemplateConfigSortService oaLogTemplateConfigSortService;


    @PostMapping("/queryTemplateList")
    @ApiOperation("后台获取日志模板list")
    public Result<BasePage<OaLogTemplateConfigListVO>> queryTemplateList(@RequestBody OaLogTemplateConfigQueryBO page) {
        page.setIsPart(false);
        return Result.ok(oaLogTemplateConfigService.queryList(page));
    }

    @PostMapping("/addTemplate")
    @ApiOperation("保存或者编辑日志模板-提交数据")
    @OperateLog(apply = ApplyEnum.OFFICE_MANAGEMENT, object = OperateObjectEnum.LOG_TEMPLATE, type = OperateTypeEnum.SETTING)
    public Result<String> addTemplate(@RequestBody OaLogTemplateConfigSaveBO saveBO) {
        OperationLog operationLog = oaLogTemplateConfigService.addOrUpdate(saveBO);
        return OperationResult.ok(operationLog);
    }

    @PostMapping("/detail/{configId}")
    @ApiOperation("编辑时获取单个日志模板数据")
    public Result<OaLogTemplateConfigVO> detail(@ApiParam("日志模板id") @PathVariable("configId") Long configId) {
        OaLogTemplateConfigVO vo = oaLogTemplateConfigService.detail(configId, true);
        return Result.ok(vo);
    }

    @PostMapping("/updateLogTemplateStatus/{configId}")
    @ApiOperation(value = "管理后台修改日志模板状态")
    @OperateLog(apply = ApplyEnum.OFFICE_MANAGEMENT, object = OperateObjectEnum.LOG_TEMPLATE, type = OperateTypeEnum.SETTING)
    public Result updateLogTemplateStatus(@ApiParam("日志模板ID") @PathVariable("configId") Long configId, @ApiParam("状态 1 启用 2 停用 3 删除") @RequestParam("status") Integer status) {
        OperationLog operationLog = oaLogTemplateConfigService.updateStatus(configId, status);
        return OperationResult.ok(operationLog);
    }

    @PostMapping("/queryPartList")
    @ApiOperation("查询正常日志模板列表")
    public Result<BasePage<OaLogTemplateConfigListVO>> queryPartList(@RequestBody OaLogTemplateConfigQueryBO queryBO) {
        queryBO.setIsPart(true);
        BasePage<OaLogTemplateConfigListVO> voBasePage = oaLogTemplateConfigService.queryList(queryBO);
        return Result.ok(voBasePage);
    }

    @ApiOperation("保存个人日志模板的排序")
    @PostMapping("/saveTemplateSort")
    public Result saveTemplateSort(@RequestBody List<OaLogTemplateConfigSort> sortList) {
        oaLogTemplateConfigSortService.saveOrUpdateSort(sortList);
        return Result.ok();
    }


}
